package io.renren.common.utils;

import java.util.HashSet;
/**
 * @Author wangfj
 * @Description 获取随机数工具类
 * @Date 9:10 2019/7/17 0017
 * @Param
 * @return
 **/
public class RandomUtil {
    public static Integer randomOfNumber(Integer size){
        int random = (int)(Math.random()*size);
        return random;
    }
/**
 * @Author wangfj
 * @Description 获取随机数方法，amount为获取随机数的个数，size为区间范围：[0，size)
 * @Date 9:10 2019/7/17 0017
 * @Param [amount, size]
 * @return java.util.HashSet<java.lang.Integer>
 **/
    public static HashSet<Integer> getNoRepeatRandom(Integer amount, Integer size) {
        HashSet<Integer> numberSet = new HashSet<>();
        if(amount <= size){
            int flag = 0;
            while (numberSet.size()<amount){
                Integer i = randomOfNumber(size);
                numberSet.add(i);
                System.out.println("此次随机数为:"+i);
                flag++;
            }
            System.out.println("循环次数为"+flag);
            System.out.print("获得的随机数为：");
            for(Integer i:numberSet){
                System.out.print(i+",");
            }
        }else {
            for(int i = 0;i<size;i++){
                numberSet.add(i);
            }
            System.out.print("获得的随机数为：");
            for(Integer i:numberSet){
                System.out.print(i+",");
            }
        }
        return numberSet;
    }
}
